package com.example.asa;

import com.example.asa.Disaster;
import com.example.asa.DisasterDAO;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/disasters")
public class DisasterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 初始化 DAO 对象
            DisasterDAO disasterDAO = new DisasterDAO();

            // 调用方法查询所有灾情信息
            List<Disaster> disasterList = disasterDAO.getAllDisasters();

            // 将数据传递给 JSP 页面
            request.setAttribute("disasterList", disasterList);

            // 转发到 JSP 页面显示
            request.getRequestDispatcher("disasterList.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "加载灾情信息失败：" + e.getMessage());
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
